package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsMchntAuthCtrlMapper;
import cc.rengu.igas.bsps.common.entity.MchntAuthCtrl;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

public class BspsMchntAuthCtrlMapperImpl implements BspsMchntAuthCtrlMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public MchntAuthCtrl selectBspsMchntAuthCtrlByPrimaryKey(String instId, String ctrlType, String mchntNo, String termNo, String txnNum, String bizType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntAuthCtrlByPrimaryKey");
        MchntAuthCtrl mchntAuthCtrl = new MchntAuthCtrl();
        mchntAuthCtrl.setInstId(instId);
        mchntAuthCtrl.setCtrlType(ctrlType);
        mchntAuthCtrl.setMchntNo(mchntNo);
        mchntAuthCtrl.setTermNo(termNo);
        mchntAuthCtrl.setTxnNum(txnNum);
        mchntAuthCtrl.setBizType(bizType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntAuthCtrl, mchntAuthCtrl);
        if (0 != iReturnCode) {
            rglog.error("获取商户业务权限控制信息失败,instId:<{}>,ctrlType:<{}>,mchntNo:<{}>,termNo:<{}>,txnNum:<{}>,bizType:<{}>,iReturnCode:<{}>", instId, ctrlType, mchntNo, termNo, txnNum, bizType, iReturnCode);
            return null;
        }
        return mchntAuthCtrl;
    }
}
